<?php


namespace app\common\service\order\coupon_order\jd_union;

use app\api\controller\Coupon;
use app\common\service\order\coupon_order\CouponOrderEntityInterface;
use app\model\CouponOrder;


class JingDongUnionSyncOrder extends JingDongUnionBase implements CouponOrderEntityInterface
{

    /**
     * 拉取订单
     * @return mixed
     */
    public function syncOrder()
    {
        /**
         * 文档：https://union.jd.com/openplatform/api/12707
         * 状态：
         * @var CouponOrder $couponOrder
         */
        $couponOrder = $this->saveOrder();

        //订单状态
        if ($couponOrder->status != $this->status) {
            $couponOrder->status = $this->status;
            $couponOrder->save();
        }

        //结算时间
        if ($couponOrder->settled_at != $this->settled_at) {
            $couponOrder->settled_at = $this->settled_at;
            $couponOrder->save();
        }

        //代理结算状态
        if ($couponOrder->agent_settle_status != $this->agent_settle_status) {
            $couponOrder->agent_settle_status = $this->agent_settle_status;
            $couponOrder->order_finish_at = date('Y-m-d H:i:s');
            $couponOrder->save();
        }

        //代理佣金
        if ($couponOrder->profit != $this->profit) {
            $couponOrder->profit = $this->profit;
            $couponOrder->save();
        }

        //平台佣金
        if ($couponOrder->platform_profit != $this->platform_profit){
            $couponOrder->platform_profit = $this->platform_profit;
            $couponOrder->save();
        }

        //退款状态
        if ($couponOrder->refund_status != $this->refund_status){
            $couponOrder->refund_status = $this->refund_status;
            $couponOrder->save();
        }

        //退款金额
        if ($couponOrder->refund_amount != $this->refund_amount) {
            $couponOrder->refund_amount = $this->refund_amount;
            $couponOrder->save();
        }

        //支付时间
        if ($couponOrder->pay_time != $this->pay_time) {
            $couponOrder->pay_time = $this->pay_time;
            $couponOrder->save();
        }

        //支付时间
        if ($couponOrder->pay_status != $this->pay_status) {
            $couponOrder->pay_status = $this->pay_status;
            $couponOrder->save();
        }

    }

    /**
     * 获取供应商类型
     * @return integer
     */
    public function getServType()
    {
        return $this->extend->supplierInfo->serv_type;
    }

    /**
     * 获取产品名称
     * @return string
     */
    public function getProductName()
    {
        return $this->params['skuName']??'';
    }

    /**
     * 获取产品图片
     * @return string
     */

    public function getProductImage()
    {
        return $this->params['goodsInfo']['imageUrl']??'';
    }

    /**
     * 获取结算状态
     * https://pic.rmb.bdstatic.com/bjh/2265d040b1c0acaaaba180b1e8030577.jpeg
     * @return integer
     * 17 已完成
     */
     public function getAgentSettleStatus()
     {
         $status = $this->params['validCode']??'';
         if ($status == 17){ //订单已完成

             return  CouponOrder::C_AGENT_SETTLE_STATUS_WAIT;
         }

         return  CouponOrder::C_AGENT_SETTLE_STATUS_NO;
     }

    /**
     * 获取状态
     * https://pic.rmb.bdstatic.com/bjh/2265d040b1c0acaaaba180b1e8030577.jpeg
     * 16 已付款
     * 17 已完成
     */
     public function getStatus()
     {
         /**
          * @var CouponOrder $couponOrder
          */
         $status = $this->params['validCode']??'';
         if (in_array($status,[16,17])){  //已支付

             return CouponOrder::C_STATUS_RECHARGE_SUCCESS;

         } elseif($status == 15) {

             return CouponOrder::C_STATUS_NO_SUBMIT;
             
         }

         return  CouponOrder::C_STATUS_RECHARGE_FAIL;
     }



}